// -----------------------------------------------------------------------
// <copyright file="AspNetUsersInRolesEN.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITIES
{
    using System;
    using System.Data.SqlClient;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    
    /// <summary>
    /// Class AspNetUsersInRoles
    /// </summary>
    public class AspNetUsersInRolesEN : EntityEN
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AspNetUsersInRolesEN" /> class
        /// </summary>
        public AspNetUsersInRolesEN()
        {
            this.UserId = Guid.Empty;
            this.RoleId = Guid.Empty;
            this.UserDepartmentId = 0;
        }

        /// <summary>
        /// Gets or sets UserId
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// Gets or sets RoleId
        /// </summary>
        public Guid RoleId { get; set; }

        /// <summary>
        /// Gets or sets DepartmentId
        /// </summary>
        public int UserDepartmentId { get; set; }

        /// <summary>
        /// Gets Department
        /// </summary>
        public DepartmentEN Department
        {
            get
            {
                try
                {
                    DepartmentEN department = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDepartmentGetById, this.UserDepartmentId);

                    while (reader.Read())
                    {
                        department = new DepartmentEN();

                        if (!reader.IsDBNull(0))
                        {
                            department.DepartmentId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            department.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            department.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            department.Parent = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            department.Order = reader.GetInt32(4);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return department;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets the role.
        /// </summary>
        /// <value>
        /// The role.
        /// </value>
        /// <exception cref="System.Exception"> </exception>
        public AspNetRolesEN Role
        {
            get
            {
                try
                {
                    AspNetRolesEN aspNetRoles = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAspNetUsersInRolesByRoleId, this.RoleId);

                    while (reader.Read())
                    {
                        aspNetRoles = new AspNetRolesEN();

                        if (!reader.IsDBNull(0))
                        {
                            aspNetRoles.ApplicationId = reader.GetGuid(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            aspNetRoles.RoleId = reader.GetGuid(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            aspNetRoles.RoleName = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            aspNetRoles.LoweredRoleName = reader.GetString(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            aspNetRoles.Description = reader.GetString(4);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return aspNetRoles;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
    }
}
